{	Problem: http://codeforces.ru/problemset/problem/17/B
	Verdict: Accepted
}
var i,n,mi,m,k,x,y,c:longint; a:array[1..1010] of longint; f:boolean;
begin
read(n);
m:=-1;
for i:=1 to n do begin
  read(k);
  if k>m then begin
    m:=k;
    mi:=i;
  end;
  a[i]:=-1;
end;
read(m);
for i:=1 to m do begin
  read(x,y,c);
  if (a[y]=-1) or (a[y]>c) then a[y]:=c;
end;
c:=0;
f:=true;
for i:=1 to n do
  if (a[i]=-1) and (i<>mi) then f:=false else
  if a[i]<>-1 then inc(c,a[i]);
if f then write(c) else write(-1);
end.